home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 3.3 KB | 153 lines | [TEXT/CWIE] |
- // FileAccessPath.cp
-
- #ifndef FileAccessPath_h
- #include "FileAccessPath.h"
- #endif
- #ifndef FileLocation_h
- #include "FileLocation.h"
- #endif
- #ifndef FileNotFoundError_h
- #include "FileNotFoundError.h"
- #endif
- #ifndef DirectoryNotFoundError_h
- #include "DirectoryNotFoundError.h"
- #endif
- #ifndef DiskFullError_h
- #include "DiskFullError.h"
- #endif
- #ifndef FileLockError_h
- #include "FileLockError.h"
- #endif
- #ifndef FileBusyError_h
- #include "FileBusyError.h"
- #endif
- #ifndef FilePermissionError_h
- #include "FilePermissionError.h"
- #endif
- #ifndef HardwareVolumeLockError_h
- #include "HardwareVolumeLockError.h"
- #endif
- #ifndef SoftwareVolumeLockError_h
- #include "SoftwareVolumeLockError.h"
- #endif
-
- FileAccessPath::FileAccessPath()
- : isOpen( false )
- {
- }
-
- FileAccessPath::FileAccessPath( const FileLocation& location,
- FilePermission permission,
- DataFork )
- : isOpen( false )
- {
- Open( location, permission, dataFork );
- }
-
- FileAccessPath::FileAccessPath( const FileLocation& location,
- FilePermission permission,
- ResourceFork )
- : isOpen( false )
- {
- Open( location, permission, resourceFork );
- }
-
- FileAccessPath::~FileAccessPath()
- {
- Assert( !IsOpen() );
- if ( IsOpen() )
- try
- {
- Close();
- }
- catch ( ... )
- {
- }
- }
-
- void FileAccessPath::Open( const FileLocation& location,
- FilePermission permission,
- DataFork )
- {
- Assert( !IsOpen() );
- Assert( location.NameIsValid() );
- ThrowError( FSpOpenDF( &location, permission.Value(), &refNum ) );
- isOpen = true;
- }
-
- void FileAccessPath::Open( const FileLocation& location,
- FilePermission permission,
- ResourceFork )
- {
- Assert( !IsOpen() );
- Assert( location.NameIsValid() );
- ThrowError( FSpOpenRF( &location, permission.Value(), &refNum ) );
- isOpen = true;
- }
-
- void FileAccessPath::Close()
- {
- Assert( IsOpen() );
- isOpen = false;
- ThrowError( FSClose( refNum ) );
- }
-
- uint32 FileAccessPath::Length() const
- {
- Assert( IsOpen() );
- int32 length;
- ThrowError( GetEOF( refNum, &length ) );
- return length;
- }
-
- void FileAccessPath::SetLength( uint32 length )
- {
- Assert( IsOpen() );
- ThrowError( SetEOF( refNum, length ) );
- }
-
- uint32 FileAccessPath::Allocate( uint32 amount )
- {
- Assert( IsOpen() );
- ThrowError( ::Allocate( refNum, reinterpret_cast<int32*>(&amount) ) );
- return amount;
- }
-
- uint32 FileAccessPath::AllocateContiguous( uint32 amount )
- {
- Assert( IsOpen() );
- ThrowError( AllocContig( refNum, reinterpret_cast<int32*>(&amount) ) );
- return amount;
- }
-
- void FileAccessPath::Flush() const
- {
- ParamBlockRec block;
- block.ioParam.ioCompletion = 0;
- block.ioParam.ioRefNum = refNum;
- ThrowError( PBFlushFileSync( &block ) );
- }
-
- void FileAccessPath::ThrowError( OSErr error )
- {
- if ( error == noErr )
- return;
-
- switch ( error )
- {
- case fnfErr: throw FileNotFoundError( error );
- case dirNFErr: throw DirectoryNotFoundError( error );
- case dskFulErr: throw DiskFullError( error );
- case permErr: throw FileLockError( error );
- case opWrErr: throw FileBusyError( error );
- case afpAccessDenied: throw FilePermissionError( error );
-
- case wPrErr: throw HardwareVolumeLockError( error );
- case vLckdErr: throw SoftwareVolumeLockError( error );
- case fLckdErr: throw FileLockError( error );
-
- }
-
- throw FileError( error );
- }
-